#include "CExpandButton.h"
#include "ElementForm.h"
#include "CTextElement.h"
#include "CIconElement.h"
CExpandButton::CExpandButton(const QString& name, const QString& iPath,const int & index, const bool& bPreview, const QStringList& values, const QString& cmdKey,  const QString& iPrifix)
	:CSkipButton(name, iPath,index),_values(values),_cmdKey(cmdKey),_bPreview(bPreview)
{

}

CExpandButton::~CExpandButton()
{
}

void CExpandButton::drawElement(QWidget* widget, QPainter* painter, bool bSelected, const QRect& rect)
{
	initTimer(widget);
	_isDrawFlag = true;
	_bSelected=bSelected;
	this->setRect(rect);
	CSkipButton::drawIcon(painter);
	drawText(painter);
	_isDrawFlag = false;
}

QList<UIElement*> CExpandButton::convertToElements()
{
	QList<UIElement*> list;
	if (_iPrifix.isEmpty()) {
		for (int i = 0; i < _values.size(); ++i) {
			QString name = getTranslate(_values.at(i));
			QString cmdKey = _cmdKey;
			QString iconPath = _iPrefixPath + QString("/s/%1s.png").arg(_index);
			
			list.push_back(new CTextElement(_values.at(i), iconPath, i, _cmdKey));
		}
	}else{
		for (int i = 0; i < _values.size(); ++i) {
			QString name = getTranslate(_values.at(i));
			QString cmdKey = _cmdKey;
			QString iconPath = _iPrefixPath + QString("/s/%1s.png").arg(_index);
			list.push_back(new CIconElement(_iPrefixPath+ _iPrifix, iconPath,i, _cmdKey));
		}
	}
	return list;
}


void CExpandButton::drawText(QPainter* painter)
{
	if (!_bSelected)
		return;
	QRect textRect;
	if (_drawLength > 0) {
		
		QFont font;
		font.setPixelSize(36);
		font.setFamily("Source Han Sans K");
		painter->setFont(font);
		QPen pen(QColor(255, 255, 255));
		painter->setPen(pen);
		QFontMetrics metrics(font);
		QString text(getTranslate(_name));

		int w = metrics.width(text);

		textRect=QRect(_pixRect.topRight() + QPoint(8, 15), QSize(_rect.width() - _pixRect.width() - 8, metrics.height()));
		if (w > textRect.width() && _timer != nullptr) {
			if (!_timer->isActive()) {

				_timer->start(_timerInterval);
			}
		}
		painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text.right(_drawLength));
	}
	{
		QFont font;
		font.setPixelSize(24);
		font.setFamily("Source Han Sans K");
		painter->setFont(font);
		QPen pen(QColor(194, 194, 194));
		painter->setPen(pen);
		QFontMetrics metrics(font);
		QString text(getTranslate(_values.at(getCommandKeyIndex(_cmdKey))));
		int w = metrics.width(text);
		painter->drawText(QRect(textRect.bottomLeft()+QPoint(4,14),QSize(w,metrics.height())), Qt::AlignLeft | Qt::AlignVCenter, text);
	}
}
